package org.dromara.stock.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.stock.domain.bo.StockInfoBo;
import org.dromara.stock.domain.vo.StockInfoVo;
import org.dromara.stock.domain.StockInfo;
import org.dromara.stock.mapper.StockInfoMapper;
import org.dromara.stock.service.IStockInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 股票详情Service业务层处理
 *
 * @author dongfs
 * @date 2024-06-30
 */
@RequiredArgsConstructor
@Service
public class StockInfoServiceImpl implements IStockInfoService {

    private final StockInfoMapper baseMapper;

    /**
     * 查询股票详情
     */
    @Override
    public StockInfoVo queryById(String code){
        return baseMapper.selectVoById(code);
    }

    /**
     * 查询股票详情列表
     */
    @Override
    public TableDataInfo<StockInfoVo> queryPageList(StockInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StockInfo> lqw = buildQueryWrapper(bo);
        Page<StockInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询股票详情列表
     */
    @Override
    public List<StockInfoVo> queryList(StockInfoBo bo) {
        LambdaQueryWrapper<StockInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StockInfo> buildQueryWrapper(StockInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StockInfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), StockInfo::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), StockInfo::getType, bo.getType());
        lqw.eq(bo.getCurrentPrice() != null, StockInfo::getCurrentPrice, bo.getCurrentPrice());
        lqw.eq(bo.getOpenPrice() != null, StockInfo::getOpenPrice, bo.getOpenPrice());
        lqw.eq(bo.getClosePrice() != null, StockInfo::getClosePrice, bo.getClosePrice());
        lqw.eq(bo.getHighPrice() != null, StockInfo::getHighPrice, bo.getHighPrice());
        lqw.eq(bo.getLowPrice() != null, StockInfo::getLowPrice, bo.getLowPrice());
        lqw.eq(bo.getChangePrice() != null, StockInfo::getChangePrice, bo.getChangePrice());
        lqw.eq(bo.getChangePercent() != null, StockInfo::getChangePercent, bo.getChangePercent());
        lqw.eq(bo.getTurnoverRate() != null, StockInfo::getTurnoverRate, bo.getTurnoverRate());
        lqw.eq(bo.getVolume() != null, StockInfo::getVolume, bo.getVolume());
        lqw.eq(bo.getTotalWorth() != null, StockInfo::getTotalWorth, bo.getTotalWorth());
        return lqw;
    }

    /**
     * 新增股票详情
     */
    @Override
    public Boolean insertByBo(StockInfoBo bo) {
        StockInfo add = MapstructUtils.convert(bo, StockInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCode(add.getCode());
        }
        return flag;
    }

    /**
     * 修改股票详情
     */
    @Override
    public Boolean updateByBo(StockInfoBo bo) {
        StockInfo update = MapstructUtils.convert(bo, StockInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StockInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除股票详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
